/*******************************************************************************
 * The MOSES project, model based software engineering of networked embedded systems.
 * 
 * Copyright (C) 2010-2013, University of Applied Sciences Wildau,
 * Telematics Research Group, http://www.tm.th-wildau.de/research-group
 * 
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as
 * published by the Free Software Foundation; either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 * 
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, see <http://www.gnu.org/licenses>.
 * 
 * Contributors:
 *     University of Applied Sciences Wildau - initial API and implementation
 * 
 * If you are interested in a commercial license please contact us via 
 * e-mail: ralf.vandenhouten@th-wildau.de.
 ******************************************************************************/
package de.thwildau.tm.moses.thrift.scoping;

import static java.util.Collections.emptyList;
import static java.util.Collections.unmodifiableList;
import static org.eclipse.xtext.util.SimpleAttributeResolver.newResolver;
import static org.eclipse.xtext.util.Strings.isEmpty;
import static org.eclipse.xtext.util.Tuples.pair;

import java.util.ArrayList;
import java.util.List;

import org.eclipse.emf.ecore.EObject;
import org.eclipse.xtext.naming.IQualifiedNameConverter;
import org.eclipse.xtext.naming.QualifiedName;
import org.eclipse.xtext.util.IResourceScopeCache;
import org.eclipse.xtext.util.Pair;

import com.google.common.base.Function;
import com.google.inject.Inject;
import com.google.inject.Provider;

/**
 * Provides alternative qualified names for Thrift definitions.
 * <p>
 * For example, given the following Thrift definition (filename shared.thrift):
 * 
 * <pre>
 * struct SharedStruct {
 *   1: i32 key
 *   2: string value
 * }
 * 
 * service SharedService {
 *   SharedStruct getStruct(1: i32 key)
 * }
 * </pre>
 * 
 * The default qualified name for {@code SharedStruct} is {@code shared.SharedStruct}. Thrift also recognizes
 * the following as qualified names:
 * <ul>
 * <li>{@code SharedStruct}</li>
 * <li>{@code shared.SharedStruct}</li>
 * <li>{@code SharedService}</li>
 * <li>{@code shared.SharedService}</li>
 * </ul>
 * </p>
 * <p>
 * This class provides the non-default qualified names recognized by the Thrift compiler.
 * </p>
 * 
 * @author Thomas Kistel
 * @version $Revision: 759 $
 */
class LocalNamesProvider {

    @Inject
    private final IResourceScopeCache cache = IResourceScopeCache.NullImpl.INSTANCE;
    @Inject
    private final IQualifiedNameConverter converter = new IQualifiedNameConverter.DefaultImpl();

    private final Function<EObject, String> resolver = newResolver(String.class, "name");


    List<QualifiedName> namesOf(final EObject obj) {
        Pair<EObject, String> key = pair(obj, "localFqns");
        return cache.get(key, obj.eResource(), new Provider<List<QualifiedName>>() {

            public List<QualifiedName> get() {
                List<QualifiedName> allNames = new ArrayList<QualifiedName>();
                EObject current = obj;
                String name = resolver.apply(current);
                if (isEmpty(name))
                    return emptyList();
                QualifiedName qualifiedName = converter.toQualifiedName(name);
                allNames.add(qualifiedName);
                while (current.eContainer() != null) {
                    current = current.eContainer();
                    String containerName = resolver.apply(current);
                    if (isEmpty(containerName))
                        continue;
                    qualifiedName = converter.toQualifiedName(containerName).append(qualifiedName);
                    allNames.add(qualifiedName);
                }
                return unmodifiableList(allNames);
            }
        });
    }
}
